{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "5c2a5add",
   "metadata": {},
   "source": [
    "# 数字\n",
    "\n",
    "## 整型\n",
    "\n",
    "整型支持基本的加（`+`）、减（`-`）、乘（`*`）、除（`/`）四则运算："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "93caecca",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "2 + 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "17c59ead",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-1"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "3 - 4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "671f9420",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "20"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "4 * 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "b4ab5062",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4.0"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "12 / 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "54ac4ba7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2.4"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "12 / 5"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b174c1bf",
   "metadata": {},
   "source": [
    "整数除法："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "955849aa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "12 // 5"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7cbb01f9",
   "metadata": {},
   "source": [
    "取余（`%`）和幂指数（`**`）运算："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "546de80a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "32 % 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "4bae0e46",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1000"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "10 ** 3"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d4c645a4",
   "metadata": {},
   "source": [
    "## 浮点型\n",
    "\n",
    "对应数学世界中的实数，在Python中也支持加减乘除的运算，包括整除和取余："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "6b882fd6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.19999999999999973"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "3.4 - 3.2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "7da485f7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "44.3"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "12.3 + 32"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "f3519d00",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6.25"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "2.5 ** 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "ece6e82c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6.05"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "12.1 / 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "50f6f8d3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6.0"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "12.1 // 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "db2691db",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.09999999999999964"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "12.1 % 2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "734cf79f",
   "metadata": {},
   "source": [
    "## 复数型\n",
    "\n",
    "用`j`表示虚部："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "7d66c13a",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 1 + 2j"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "056967f3",
   "metadata": {},
   "source": [
    "复共轭："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "8329a3a8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1-2j)"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.conjugate()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b06d7a5d",
   "metadata": {},
   "source": [
    "## 布尔型\n",
    "\n",
    "取值真（`True`，等于1）假（`False`，等于0）："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "74e22c44",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "1 > 2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9a134752",
   "metadata": {},
   "source": [
    "链式比较："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "6333111d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = 2\n",
    "1 < a <= 4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "f366fc64",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "1 + True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "be037a35",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "2 + False"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "40944bdc",
   "metadata": {},
   "source": [
    "## 混合运算\n",
    "\n",
    "多个数学运算组合："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "596e51ac",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-27.0"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "1 + 2 - (3 * 4 / 6) ** 5 + 7 % 5"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0e87756e",
   "metadata": {},
   "source": [
    "先括号里面，再括号外面；先算幂指数，再乘除，最后加减。\n",
    "\n",
    "## 原地运算\n",
    "\n",
    "使用操作符与等于连用："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "30bb0d8f",
   "metadata": {},
   "outputs": [],
   "source": [
    "b = 2.5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "f6a70b25",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4.5"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b += 2\n",
    "b"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cd351de5",
   "metadata": {},
   "source": [
    "## 数学函数\n",
    "\n",
    "绝对值："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "accb2351",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "12"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "abs(-12)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "61735ed7",
   "metadata": {},
   "source": [
    "取整："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "25424fa6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "13"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "round(12.6)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "635b3856",
   "metadata": {},
   "source": [
    "最值："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "303fa328",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "max(1, 2, 3, 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "f839991a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "min(2, 0, 1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d7038b04",
   "metadata": {},
   "source": [
    "## 特殊类型的数字表示\n",
    "\n",
    "二进制："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "139b4f15",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "33"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "0b100001"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ace3d500",
   "metadata": {},
   "source": [
    "八进制："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "3fb0db88",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "447"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "0o677"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7ca54b40",
   "metadata": {},
   "source": [
    "十六进制："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "9fec4453",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8191"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "0x1fff"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e9679867",
   "metadata": {},
   "source": [
    "科学计数法："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "f2ce7080",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.0102"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "10.2e-3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4e1b9a17",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
